Review from last lecture

The heap is slow because of fragmentation - data can be like this:

Then, B is deleted, creating a "memory hole":

This causes searching to take longer.

Stack and functions

Stack function calling example

Some code examples:

// huge static array
// if size is too big, could cause stack overflow
double nums[1000];

// dynamic array (NOT covered [prohibited in fact] in this class)
double nums[x]; // where x is defined earlier

// dynamically allocated array (the correct way to do it)
vector<double> nums[100000000];

// incorrect way to do it - static array would be too big
// and this would cause a stack overflow
double* nums = new double[100000000];

Another way to cause a stack overflow is with recursive functions:

Frames:

Frame and stack pointers

Functions